package net.tuanhiep1232.medicine.client.menu;

import java.util.ArrayList;
import java.util.List;

import net.tuanhiep1232.medicine.client.custom.RibbonMenuButton;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.container.Container;

public class RibbonMenu extends FlowPanel{
	private Widget contentPanel;
	private List<RibbonMenuButton> buttons = new ArrayList<RibbonMenuButton>();
	private List<Widget> widgets = new ArrayList<Widget>();
	
	public RibbonMenu(String divName) {
		super();
		this.contentPanel = new FlowPanel();
		this.setHeight("100%");
//		this.getElement().getStyle().setHeight(100, Unit.PC);
	}
	public RibbonMenu(Panel contentPanel) {
		super();
		this.contentPanel = contentPanel;
		this.setHeight("100%");
		this.getElement().getStyle().setHeight(100, Unit.PC);
	}
	public RibbonMenu(Container contentPanel) {
		super();
		this.contentPanel = contentPanel;
	}
	public Widget getContentPanel() {
		return contentPanel;
	}
	public void setContentPanel(Panel contentPanel) {
		this.contentPanel = contentPanel;
	}
	public void setContentPanel(Container contentPanel) {
		this.contentPanel = contentPanel;
	}
	public List<RibbonMenuButton> getButtons() {
		return buttons;
	}
	public void setButtons(List<RibbonMenuButton> buttons) {
		this.buttons = buttons;
	}
	
	public List<Widget> getWidgets() {
		return widgets;
	}
	public void setWidgets(List<Widget> widgets) {
		this.widgets = widgets;
	}
	public void addMenu(String text,String color,final Widget widget){
		final RibbonMenuButton ribbonMenuButton = new RibbonMenuButton(text, color);
		this.buttons.add(ribbonMenuButton);
		this.add(ribbonMenuButton);
		ribbonMenuButton.getTxt().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent arg0) {
				buttonClick(ribbonMenuButton, widget);
			}
		});
		if(this.buttons.size()==1) buttonClick(ribbonMenuButton, widget);
	}
	
	public void removeMenu(int index){
		if(this.buttons.get(index).isSelected()){
			if(this.buttons.size()>1){
				if(index==0)buttonClick(1);
				else buttonClick(index-1);
			}
		}
		//TODO remove handler for button
		this.buttons.remove(index);
		this.widgets.remove(index);
	}
	
	private void buttonClick(RibbonMenuButton button,Widget widget){
		button.setSelected(true);
		if(contentPanel instanceof Panel) ((Panel)contentPanel).add(widget);
		if(contentPanel instanceof Container) ((Container)contentPanel).add(widget);
	}
	private void buttonClick(int index){
		RibbonMenuButton button = this.buttons.get(index);
		Widget widget = this.widgets.get(index);
		button.setSelected(true);
		if(contentPanel instanceof Panel) ((Panel)contentPanel).add(widget);
		if(contentPanel instanceof Container) ((Panel)contentPanel).add(widget);
	}
}
